package org.client.chat;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;

import org.model.ChatStrings;
import org.model.Friend;
import org.secure.SecureInputStream;
import org.secure.SecureOutputStream;

public class ClientChatingWindow extends ChatingWindow implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Friend mFriend;
	private SecureOutputStream outputStream;
	private SecureInputStream inputStream;
	boolean isSending = false;
	private String loginName;
	private Socket clientSocket;
	private byte[] key;
	public ClientChatingWindow(Friend friend, String loginName, byte[] key)
	{
		super(friend.getName());
		this.loginName = loginName;
		mFriend = friend;		
		sendButton.addActionListener(this);
		this.key = key;
	}
	private class ChatListener extends Thread
	{
		private SecureInputStream inputStream;
		public ChatListener(SecureInputStream is)
		{
			inputStream =is;
		}
		@Override
		public void run() {
			super.run();
			try {
				while(true)
				{
					String line = inputStream.readLine();
					if(checkIfEndChat(line))
						break;
					if(isSending)
					{
						chatArea.append(mFriend.getName()+ChatStrings.USER_NEXT+line+"\n");
						isSending = false;
					}else
						chatArea.append(ChatStrings.SAPCES+line+"\n");
					
					chatArea.setCaretPosition(chatArea.getText().length());
				}
			} catch (Exception e) {
				//e.printStackTrace();
				System.out.println("you parnter has closed chating");
			}
		}
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		try {
			if(clientSocket == null)
			{
				InetAddress address = InetAddress.getByAddress(mFriend.getAddress().getIpAddress());
				clientSocket = new Socket(address,mFriend.getAddress().getPort());
				outputStream = new SecureOutputStream(clientSocket.getOutputStream());
				inputStream = new SecureInputStream(new InputStreamReader(clientSocket.getInputStream()));
				outputStream.setPrivateKey(key);
				inputStream.setPrivateKey(key);
				outputStream.writeString(loginName+ChatStrings.END_LINE);
				outputStream.setClient(true);
				inputStream.setClient(true);
				new ChatListener(inputStream).start();
			}
			String  line = sendField.getText();
			if(line == null || line.compareTo("") == 0)
				return;
			if(!isSending)
			{
				chatArea.append(loginName+ChatStrings.USER_NEXT+line+"\n");
				isSending = true;
			}
			else
				chatArea.append(ChatStrings.SAPCES+line+"\n");
			chatArea.setCaretPosition(chatArea.getText().length());
			outputStream.writeString(line+ChatStrings.END_LINE);	
		} catch (Exception e) {
			System.err.print("partner is closed");
		}
	}
	@Override
	public void windowClosing(WindowEvent e) {
		try {
			if(clientSocket != null)
			{
				byte[] end = new byte[3];
				end[0] = 1;
				end[1] = 2;
				end[2] = 3;
				String bytes = new String(end);
				outputStream.writeString(bytes+ChatStrings.END_LINE);
				clientSocket.close();
			}
		} catch (Exception e2) {
			e2.printStackTrace();
		}
		
	}
	
}
